package com.gxa.controller;

import com.gxa.pojo.Role;
import com.gxa.pojo.User;
import com.gxa.service.LogService;
import com.gxa.service.UserService;
import com.gxa.utils.JsonResult;
import org.omg.CORBA.MARSHAL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @Autowired
    UserService userService;
    @Autowired
    LogService logService;


    JsonResult jsonResult;

    @ResponseBody
    @RequestMapping("/userPage")
    public Map findUserByPage(@RequestBody Map map){
        jsonResult=new JsonResult();
        String number=map.get("number").toString();
        String size = map.get("size").toString();
        Integer offset = (Integer.parseInt(number) - 1) * Integer.parseInt(size);
        Integer limit= Integer.valueOf(size);
        List<User> users = userService.findUserByPage(offset, limit);
        Integer count = userService.findAllUser();
        jsonResult.setData(users);
        jsonResult.put("count",count);
        return jsonResult.getValues();
    }

    @ResponseBody
    @RequestMapping("/userAdd")
    public Map insertUser(@RequestBody Map map) throws ParseException {
        jsonResult=new JsonResult();
        System.out.println(map);
        Date date=new Date();
        Map log = (Map) (map.get("log"));
        log.put("operationTime",date);
        int ii = logService.insertLog(log);
        String userName = map.get("userName").toString();
        String citizenID = map.get("citizenID").toString();
        String phone = map.get("phone").toString();
        String userByUserName = userService.findUserByUserName(userName);
        String userByIdCard = userService.findUserByIdCard(citizenID);
        String userByPhone = userService.findUserByPhone(phone);
        if (userByUserName!=null){
            jsonResult.setCode("401");
            jsonResult.setMsg("用户名重复！");
            return jsonResult.getValues();
        }
        if (userByIdCard!=null){
            jsonResult.setCode("402");
            jsonResult.setMsg("身份证号码重复！");
            return jsonResult.getValues();
        }
        if (userByPhone!=null){
            jsonResult.setCode("403");
            jsonResult.setMsg("电话号码重复！");
            return jsonResult.getValues();
        }
        if (map.get("departmentID").toString()==null){
            jsonResult.setCode("404");
            jsonResult.setMsg("部门不能为空！");
            return jsonResult.getValues();
        }if (map.get("dutyID").toString()==null){
            jsonResult.setCode("405");
            jsonResult.setMsg("职位不能为空！");
            return jsonResult.getValues();
        }
//        String s=map.get("birthDate").toString();
//        String ss=map.get("entryDate").toString();
//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
//        Date birthDate = dateFormat.parse(s);
//        Date entryDate = dateFormat.parse(ss);
        map.put("birthDate",new Date());
        map.put("entryDate",new Date());
        System.out.println(map);
        int i = userService.insertUser(map);
        if (i>0){
            jsonResult.setCode("200");
            jsonResult.setMsg("添加成功");
        }else {
            jsonResult.setCode("406");
            jsonResult.setMsg("系统开了小差");
        }
        return jsonResult.getValues();
    }

    @ResponseBody
    @RequestMapping("/userDel")
    public Map deleteUser(@RequestBody Map map){
        jsonResult=new JsonResult();
        System.out.println(map);
        Date date=new Date();
        Map log = (Map) (map.get("log"));
        log.put("operationTime",date);
        int ii = logService.insertLog(log);
        String id = map.get("userID").toString();
        Integer i = userService.deleteUserById(Integer.valueOf(id));
        if (i>0){
            jsonResult.setCode("200");
            jsonResult.setMsg("删除成功");
        }else {
            jsonResult.setCode("406");
            jsonResult.setMsg("系统开了小差");
        }
        return jsonResult.getValues();
    }

    @ResponseBody
    @RequestMapping("/userEdit")
    public Map updateUser(@RequestBody Map map){
        jsonResult =new JsonResult();
        System.out.println(map);
        Date date=new Date();
        Map log = (Map) (map.get("log"));
        log.put("operationTime",date);
        int ii = logService.insertLog(log);
        Integer i = userService.updateUser(map);
        if (i>0){
            jsonResult.setCode("200");
            jsonResult.setMsg("修改成功");
        }else {
            jsonResult.setCode("406");
            jsonResult.setMsg("系统开了小差");
        }
        return jsonResult.getValues();
    }

    @ResponseBody
    @RequestMapping("/userSeach")
    public Map selectUserByNameAndUserName(@RequestBody Map map){
        jsonResult=new JsonResult();
        Integer count = userService.findAllUserBy(map);
        System.out.println("map数据为："+map);
        String number=map.get("number").toString();
        String size = map.get("size").toString();
        Integer offset = (Integer.parseInt(number) - 1) * Integer.parseInt(size);
        Integer limit= Integer.valueOf(size);
        map.put("offset",offset);
        map.put("limit",limit);
        System.out.println(map);
        List<User> users = userService.selectUserByNameAndUserName(map);
        System.out.println(users);
        if (users!=null){
            jsonResult.setData(users);
            jsonResult.put("count",count);
            jsonResult.setCode("200");
            jsonResult.setMsg("查询成功");
        }else {
            jsonResult.setCode("406");
            jsonResult.setMsg("系统开了小差！！");
        }
        return jsonResult.getValues();
    }

    @ResponseBody
    @RequestMapping("/selectUserByUserName")
    public Map selectUserByUserName(@RequestBody Map map){
        jsonResult=new JsonResult();
        List<User> users = userService.selectUserByUserName(map);
        if (users!=null){
            jsonResult.setData(users);
            jsonResult.setCode("200");
            jsonResult.setMsg("查询成功");
        }else {
            jsonResult.setCode("406");
            jsonResult.setMsg("系统开了小差");
        }
        return jsonResult.getValues();
    }

    @ResponseBody
    @RequestMapping("/userState")
    public Map updateUserState(@RequestBody Map map){
        jsonResult=new JsonResult();
        System.out.println(map);
        Date date=new Date();
        Map log = (Map) (map.get("log"));
        log.put("operationTime",date);
        int ii = logService.insertLog(log);
        Integer i = userService.updateUserState(map);
        if (i>0){
            jsonResult.setCode("200");
            jsonResult.setMsg("状态修改成功");
        }else {
            jsonResult.setCode("406");
            jsonResult.setMsg("系统开了小差");
        }
        return jsonResult.getValues();
    }

    @ResponseBody
    @RequestMapping("/updateRole")
    public Map updateRole(@RequestBody Map map){
        jsonResult=new JsonResult();
        String userID = map.get("userID").toString();
        User roleByUserId = userService.findRoleByUserId(Integer.valueOf(userID));
        List<Role> allRole = userService.findAllRole();
        jsonResult.put("roleByUserId",roleByUserId);
        jsonResult.setData(allRole);
        jsonResult.setCode("200");
        jsonResult.setMsg("检索成功");
        return jsonResult.getValues();
    }

}
